package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import model.User;
public class DaoImpl implements IUserDAO {
	private Connection con;
	private PreparedStatement stat = null;
	public DaoImpl(Connection con){
		this.con = con;
	}
	public boolean insertUser(User user)throws Exception {
		//插入新的用户
		boolean test=false;
			String sql="insert into users(Id,name,password) values(?,?,?)";
			stat=con.prepareStatement(sql);
			stat.setInt(1, user.getId());
			stat.setString(2, user.getname());
			stat.setString(3, user.getpassword());
			int insert = stat.executeUpdate();
			if(insert>0){
				test=true;
			}
			else{
				test=false;
			}
		return test;
	}
	public boolean updateUser(User user)throws Exception {
	//更新用户的信息
		boolean test=false;
			String sql="update users set name=?,password=? where Id=?";
			stat=con.prepareStatement(sql);
			stat.setString(1, user.getname());
			stat.setString(2, user.getpassword());
			stat.setInt(3, user.getId());
		int update=stat.executeUpdate();
			if(update>0)
				test=true;
		return test;
	}
	public boolean deleteUser(User user)throws Exception {
//删除用户名
		boolean test=false;
		try {
			String sql="delete from users where Id=?";
			stat=con.prepareStatement(sql);
			stat.setInt(1, user.getId());
			int delete=stat.executeUpdate();
			if(delete>0)
				test=true;
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return test;
	}
	public List<User> findAll()throws Exception{
		//查找所以用户的卡号Id、用户名name以及密码password
		String sql = "select * from users";
		stat = con.prepareStatement(sql);
		ResultSet rs = stat.executeQuery();
		User user = null;
		List<User> list = new ArrayList<User>();
		while(rs.next()){
			int Id = rs.getInt(1);
			String name = rs.getString(2);
			String password = rs.getString(3);
			user = new User();
			user.setId(Id);
			user.setname(name);
			user.setpassword(password);
			list.add(user);

		}
		return list;
	}
	public User findById(int Id)throws Exception{

		//用用户名name和密码password查找用户的卡号Id是什么。
		String sql = "select Id,name,password from users where Id=?";
		stat = con.prepareStatement(sql);
		stat.setInt(1,Id);
		ResultSet rs = stat.executeQuery();
		User user = null;
		if(rs.next()){
			String name = rs.getString(2);
			String password = rs.getString(3);
			user = new User();
			user.setId(Id);
			user.setname(name);
			user.setpassword(password);
			rs.close();
		}
		return user;
	}
	public User findByName(String name) throws Exception {
		//用用户名查找密码

		String sql = "select Id,name,password from users where name=?";
		stat = con.prepareStatement(sql);
		stat.setString(1,name);
		ResultSet rs = stat.executeQuery();
		User user = null;
		if(rs.next()){
			int Id=rs.getInt(1);
			String password = rs.getString(3);
			user = new User();
			user.setId(Id);
			user.setpassword(password);

		}
		return user;
	}

	public boolean IsName(String name) throws Exception {
		boolean test=true;
		String sql = "select Id from users where name=?";
		stat = con.prepareStatement(sql);
		stat.setString(1,name);
		ResultSet rs = stat.executeQuery();
		if(rs.next()){
			int Id = rs.getInt(1);
			if(Id!=0)
				test=false;
		}
		return test;
	}
}

